<?php

require_once($controllerDir . '/pclinkhash.php');
require_once($modelDir . '/loginmodel.php');
require_once($controllerDir . '/controller.php');
require_once($viewDir . '/loginview.php');
require_once($databaseControllerDir . '/session.php');
require_once($databaseControllerDir . '/sessionmysql.php');
require_once($databaseControllerDir . '/mysql-config.php');

define('NEW_SESSION',     001 );
define('OPENING_SESSION', 002 );
define('OPENED_SESSION',  003 );
define('CLOSED_SESSION',  004 );
define('EXPIRED_SESSION', 005 );


class LoginController  extends controller {

  protected $linkController;
  protected $loginmodel;
  protected $sessionState;
  protected $login_view;
  protected $mysql_session;

  protected $str_sessionstate = "lcSessionState";

  public function __construct($login_model) {
    parent::__construct();
    $this->connectionID = 0;
    $this->restoreController();

    $this->linkController = PCLinkHash::getInstance();
    $this->loginmodel     = $login_model;
    $this->login_view     = new loginview();

echo "user constructor: " . $this->loginmodel->getUser() . " pswd: " . $this->loginmodel->getPassword();

  }

  public function restoreController() {
    if (isset($_SESSION[$this->str_sessionstate])) {
      $this->sessionState = $_SESSION[$this->str_sessionstate];
    } else
      $this->setSessionState(NEW_SESSION);
  }

  public function getActionControllerName() {
    return "login_controller";
  }

  public function getSessionState() {
    return $this->sessionState;
  }

  public function setSessionState($session_state) {
    $this->sessionState = $session_state;
    $_SESSION[$this->str_sessionstate] = $session_state;
  }

  public function getLoginModel() {
   return $this->loginmodel;
  }

  public function invoke() {
echo "session state: " . $this->sessionState;

    if($this->sessionState == NEW_SESSION) {
      $this->viewLoginSession();
      $this->setSessionState(OPENING_SESSION);
    } else if($this->sessionState == OPENING_SESSION){
      $this->openingSessionHandler();
    } else if($this->sessionState == OPENED_SESSION){
      $this->openedSessionHandler();
    }
echo "logincontroller::invoke(...)";

    return True;
  }

  protected function openingSessionHandler() {
//   echo "OPENING SESSION STATE";

   $this->mysql_session = new sessionmysql();
   if($this->mysql_session->getConnectionID() == 0) {
     $this->mysql_session->openDBConnection(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
     if($this->mysql_session->getConnectionID() == 0) {
       header('Refresh: 3');
       die('Не удалось открыть соеденение с базой данных');
     }
   }
echo "user: " . $this->loginmodel->getUser() . " pswd: " . $this->loginmodel->getPassword();
//echo "CONNECTION TO THE DB = " . $this->mysql_session->getConnectionID();
   $result = $this->mysql_session->login($this->loginmodel->getUser(), $this->loginmodel->getPassword());
   if($result == true)
     echo "LOGIN RESULT: true";
   else
       echo "LOGIN RESULT: false";


   $result = $this->mysql_session->check_user($this->loginmodel->getUser());
   if($result == true)
      echo "CHECK USER RESULT: true";
   else
      echo "CHECK USER RESULT: false";


      /*    echo $_GET["mkey"];
          echo "<br/> status: ";
          echo $_REQUEST['action'];
          echo "<br/> status111: ";
          echo $_GET['status'];
          echo "<br/> status222: ";
          echo $_GET['action'];
          echo "<br/> status333: ";
          echo $_POST['submit'];
          echo "<br/> status444: ";
          echo $_POST['action'];
          echo "<br/> status555: ";
          echo $_POST['login_password'];
          echo "<br/> status777: ";
          echo $_POST['login_username'];
          echo "<br/> status888: ";
          echo $_GET['username'];*/
  }

  protected function openedSessionHandler() {
      echo "OPENED SESSION STATE";
  }

  protected function viewLoginSession() {
   global $viewDir;
   global $base_URL;
    $this->login_view->setBaseURL($base_URL);
    $this->login_view->setLinkController($this->linkController);
    $this->login_view->setViewDirectory($viewDir);
    $this->login_view->setActionControllerName($this->getActionControllerName());
    $this->sessionState = OPENING_SESSION;
    $this->setSessionState($this->sessionState);
echo "--->>";
    $this->login_view->invoke();
/*   include $viewDir . '/loginview.php';*/
  }

  protected function closeSession() {
/*remove all saved session states and variables*/
  }
}

?>
